package com.briup.jz.web.controller;


import com.briup.jz.bean.Address;
import com.briup.jz.bean.Category;
import com.briup.jz.bean.extend.AddressExtend;
import com.briup.jz.service.IAddressService;
import com.briup.jz.utils.Message;
import com.briup.jz.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: jz
 * @description: 地址控制器
 * @author: catmilko
 * @create: 2020/6/12 15:52
 **/
@RestController
@RequestMapping("/address")
@Api(description = "地址管理接口")
public class AddressController {
    @Autowired
    private IAddressService addressService;

    @ApiOperation(value = "查询所有地址信息")
    @GetMapping("findAll")
    public Message findAll(){
        List<Address> list = addressService.findAll();
        return MessageUtil.success(list);
    }

    @ApiOperation(value = "通过id查询地址信息")
    @ApiImplicitParam(name = "id", value = "类别唯一编号", required = true, paramType = "query")
    @GetMapping("findById")
    public Message findById(long id) {
        Address address = addressService.findById(id);
        return MessageUtil.success(address);
    }

    @ApiOperation(value = "通过用户id查询地址信息")
    @ApiImplicitParam(name = "userId", value = "用户唯一编号", required = true, paramType = "query")
    @GetMapping("findByUserId")
    public Message findByUserId(long userId) {
        List<Address> list = addressService.findByUserId(userId);
        return MessageUtil.success(list);
    }

    @ApiOperation(value = "通过id删除地址信息")
    @GetMapping("deleteById")
    public Message deleteById(long id) {
        addressService.deleteById(id);
        return MessageUtil.success("删除失败");
    }

    @ApiOperation(value = "保存或更新地址信息")
    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(Address address){
        addressService.saveOrUpdate(address);
        return MessageUtil.success("保存成功");
    }

    @ApiOperation(value = "级联查询用户地址信息")
    @GetMapping("findAllWithUser")
    public Message findAllWithUser(){
        List<AddressExtend> list = addressService.findAllWithUser();
        return MessageUtil.success(list);
    }
}
